<template>
	<om-addpage :saveBtnText="'确定'" :showHistoryBtn="false" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
		<uni-list>
			<uni-list-item :showArrow="false" :showHover="false">
				<radio-group @change="radioChange">
					<view style="display: flex; flex-flow:row wrap">
						<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in checkRadio" :key="item.value">
							<view class="tui-flex" style="padding-right: 5px;padding-top: 5px;">
								<view><radio :value="item.value" :checked="index === radioIndex" /></view>
								<view>{{ item.text }}</view>
							</view>
						</label>
					</view>
				</radio-group>
			</uni-list-item>
			<uni-list-item v-if="showChangeNode && isApprove=='1'" :showHover="false" :showArrow="changeNode">
				<view class="tui-flex align-center">
					<view class="tui-flex tui-col-6">
						<om-label label="转办" :required="changeNodeRequired"></om-label>
						<switch @change="switchChangeNodeChange" :class="changeNode ? 'checked' : ''" :checked="changeNode ? true : false"></switch>
					</view>
					<view v-if="changeNode" class="tui-right tui-col-6" @click="linkChangeNodeUserFunc">{{ changeNodeUserNames || '请选择' }}</view>
				</view>
			</uni-list-item>
			<uni-list-item v-if="showChangeApprover && isApprove=='1'" :showHover="false" :showArrow="changeApprover">
				<view class="tui-flex align-center">
					<view class="tui-col-6">
						更换审批人
						<switch @change="switchApproverChange" :class="changeApprover ? 'checked' : ''" :checked="changeApprover ? true : false"></switch>
					</view>
					<view v-if="changeApprover" class="tui-right tui-col-6" @click="linkApproveUserFunc">{{ approveUserName || '请选择' }}</view>
				</view>
			</uni-list-item>
			<uni-list-item v-if="isApprove=='4'" :showHover="false" :showArrow="false">
				<view class="tui-left" style="text-align: left;">
					{{'驳回方式'}}
				</view>
				<view class="tui-flex align-center">
					<radio-group @change="backTypeRadioChange">
						<label class="uni-list-cell uni-list-cell-pd" style="boorder: 1px solid red; display: flex;margin-top: 5px;" v-for="(item, index) in backTypeList" :key="item.value">
							<view>
								<radio :value="item.value" :checked="index === current" />
							</view>
							<view>{{item.name}}</view>
						</label>
					</radio-group>
				</view>
			</uni-list-item>
			<uni-list-item v-if="isApprove=='4'" :showHover="false">
				<view class="tui-flex align-center">
					<view class="tui-col-6">
						驳回节点
					</view>
					<view class="tui-right tui-col-6" @click="linkBackFlowNodeFunc">
						{{backFlowNodeName||"请选择"}}
					</view>
				</view>
			</uni-list-item>
			<uni-list-item  :showArrow="false" :showHover="false">
				<view style="text-align: left;width: 100%;"><textarea  :placeholder-style="isApprove=='4' && noOpinion?'border:1px solid red':''"  :auto-height="true" class="om-textarea padding" v-model="opinion" :placeholder="isApprove=='4'&& noOpinion?'项为必输项':'意见说明……'" /></view>
			</uni-list-item>
		</uni-list>
	</om-addpage>
</template>

<script>
	import { buttonShow } from '@/static/js/buttonShow.js';
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				changeNodeRequired:false,
				backFlowNodeCache:"",
				buttons:[],
				infonameId:"",
				backFlowNodeName:'',
				approveUserId:'',
				radioIndex: 0,
				busCode:"",
				fileId:"",
				fileCode:'',
				backNodeId:'',
				backUserId:'',
				checkRadio: [],
				showChangeNode: false,//显示转办
				showChangeApprover: false,//显示更换审批人
				isApprove: '1', //用户勾选
				opinion: '', //审核意见
				changeNode: false, //是否勾选“转办”
				clickBtnIndex:0,//用户点击按钮的索引值
				changeNodeUserNames:'',
				backTypeList:[{
					value:'1',
					name:'接收节点处理后，按顺序流转'
				},{
					value:'2',
					name:'接收节点处理后，直接返给我'
				}],
				current:0,
				noOpinion:false
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId','orgId'])
		},
		methods:{
			backTypeRadioChange(evt){
				let _that = this
				for (let i = 0; i < _that.backTypeList.length; i++) {
					if (_that.backTypeList[i].value === evt.detail.value) {
						_that.current = i;
						_that.backType = evt.detail.value
						break;
					}
				}
			},
			btnChange(index) {
				this.clickBtnIndex = index
				this[this.buttons[index].func](index);
			},
			linkChangeNodeUserFunc() {
				let pages = getCurrentPages();
				let page = pages[pages.length - 2];
				var url = getUrl('/pages/public/multiSearch/multiSearch', {
					other: JSON.stringify({
						flowConfigNodeId: this.buttons[this.clickBtnIndex].flowConfigNodeId
					}),
					maxSelectNum:5,
					initload:true,
					pagination:true,
					namefield: 'userPosInstName',
					searchfield: 'q',
					url: 'flowConfigNode/getChangeNodePager',
					cachename: page.flowChangeNodeCache,
					placeholder: '请输入姓名、岗位、机构'
				});
				uni.setStorage({
					key: page.flowChangeNodeCache,
					data: this.changeNodeInitData
				})
				uni.navigateTo({
					url: url
				});
			},
			switchChangeNodeChange(e) {
				this.changeNode = e.detail.value;
			},
			getWorkParam1(){
				let _that = this
				return {
					isCheck:'true'
				}
			},
			formSubmit(){
				let _that = this
				if(this.isApprove == '4' && !this.opinion){
					this.noOpinion = true
					return 
				}
				let obj = {
					formId: _that.fileId,
					clazzName: 'com.room.hr.model.TlApply',
					passOrFail: '',
					isApprove: _that.isApprove,
					approveUserId: '',				
					opinion: _that.opinion,
					backNodeId: _that.backNodeId,
					backUserId: _that.backUserId,
					businessFormCode: _that.busCode,
					fileOrgId: _that.orgId,
					PAGE_CODE: _that.busCode,
					FILE_CODE: _that.fileCode,
					objStr:_that.formdata.objStr,
					instDirectorStr:_that.formdata.instDirectorStr,
					businessDirectorStr: _that.formdata.businessDirectorStr
				}
				_that.om.request(
					"tlApply/doAudited",
					obj,
					'post'
				).then(res=>{
					if(res.succeed){
						_that.om.getFormInfo(_that.fileId, _that.busCode, true)
					}else{
						_that.om.toast(res.msg)
					}
				})
			},
			linkBackFlowNodeFunc() {
				let pages = getCurrentPages();
				let page = pages[pages.length - 2];
				if(this.isApprove === "4"){
					this.backFlowNodeCache = page.backFlowNodeCache
					let url = getUrl('/pages/public/search/search', {
						other: JSON.stringify({
							businessFormCode: this.busCode,
							formId: this.fileId
						}),
						namefield: 'role|sort',
						namesplit: '|',
						extrafield: 'userNames',
						url: 'common/getBackFlowList',
						cachename: page.backFlowNodeCache,
						placeholder: '请选择',
						initload:true,
						searchbar:false
					});
					uni.navigateTo({
						url: url
					});
				}
			},
			radioChange(e) {
				this.isApprove = e.target.value;
				for (let i = 0; i < this.checkRadio.length; i++) {
					if (this.checkRadio[i].value === e.target.value) {
						this.radioIndex = i;
						break;
					}
				}
				if(this.isApprove !== "1"){
					this.changeApprover = false
					this.approveUserId = ""
					this.approveUserName = ""
				}
				if(this.isApprove !== "4"){
					this.backNodeIndex = 0
				}
			},
			getCheckRadio(btnArr){
				let _that = this;
				this.checkRadio = btnArr;
			},
			getWorkParam(){
				let _that = this
				return {
					infonameId:_that.infonameId,
					isCheck:"true"
				}
			},
			initLoad(options){
				let _that = this
				let ajaxArr = [{
					url:'tlApply/getInfo',
					param:{
						id:options.fileId
					}
				}]
				formModelLoad(ajaxArr,function(res,mainData){
					_that.infonameId = res[0].value.tlApply.userId
					_that.subEditorObj['instDirectorInfo'].workList = res[0].value.instDirectorList.filter(e=>e.instDirectorId == _that.userId)
					_that.subEditorObj['businessDirectorInfo'].workList = res[0].value.businessDirectorList.filter(e=>e.businessDirectorId == _that.userId)
					_that.fileCode = res[0].value.tlApply.code
					changeConfigAttr('topinfo','editors',[],mainData)
					for(let item of res[1].value.bodyTitle){
						if(item.fieldId && item.fieldId !="instDirectorInfo" && item.fieldId !="businessDirectorInfo"){
							changeConfigAttr(item.fieldId,'editors',[],mainData)
						}else{
							if(item.fieldId == 'instDirectorInfo'){
								let idList = res[0].value.instDirectorList.map(e=>e.instDirectorId).join(',')
								if(idList.indexOf(_that.userId) == -1){
									changeConfigAttr(item.fieldId,'editors',[],mainData)
								}
							}
							if(item.fieldId == 'businessDirectorInfo'){
								let idList = res[0].value.businessDirectorList.map(e=>e.businessDirectorId).join(',')
								if(idList.indexOf(_that.userId) == -1){
									changeConfigAttr(item.fieldId,'editors',[],mainData)
								}
							}
						}
					}
					if(_that.subEditorObj["instDirectorInfo"]){
						let att = {
							showAdd:false,
							showDel:false
						}
						let configData = _that.subConfigData["instDirectorInfo"]
						if(hasEditor("instDirectorInfo","instDirectorName",configData)){
							att.titleTemplate="<%= instDirectorName %>"
						}
						if(hasEditor("instDirectorInfo","interviewFlag",configData)){
							att.subtitle1Template = "<%= interviewFlag?'已面谈':'未面谈'%>"
						}
						changeEditor("instDirectorInfo", "instDirectorStr",att, mainData)
					}
					if(_that.subEditorObj["businessDirectorInfo"]){
						let att = {
							showAdd:false,
							showDel:false,
							getWorkParam:"getWorkParam1"
						}
						let configData = _that.subConfigData["businessDirectorInfo"]
						if(hasEditor("businessDirectorInfo","businessDirectorId",configData)){
							att.titleTemplate="<%= businessDirectorName %>"
						}
						if(hasEditor("businessDirectorInfo","interviewFlag",configData)){
							att.subtitle1Template = "<%= interviewFlag?'已面谈':'未面谈'%>"
						}
						changeEditor("businessDirectorInfo", "businessDirectorStr",att, mainData)
					}
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			this.busCode = options.code
			this.fileId = options.fileId
			if(Object.prototype.toString.call(options.showChangeNode) == "[object Boolean]"){
				this.showChangeNode = options.showChangeNode
			}
			this.changeNodeRequired = options.changeNodeRequired
			this.buttons = JSON.parse(options.buttons)
			_that.getCheckRadio(JSON.parse(options.btnArr))
			_that.initLoad(options)
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for (let fieldId in _that.subEditorObj) {
					let editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key:_that.backFlowNodeCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							this.backFlowNodeName = d.roleSort
							this.backUserId = d.userList.map(e=>e.id).join(',')
							this.backNodeId = d.nodeId
							uni.removeStorage({
								key:_that.backFlowNodeCache
							})
						}
					}
				})
			}
		}
	}
</script>
<style></style>                                                                                                                                                                                                                                                                                                                  
